package com.gun3y.manager;

import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.gun3y.entity.Wrapper;
import com.gun3y.exception.ExceptionCode;
import com.gun3y.exception.SWException;
import com.gun3y.utils.ResourceLoader;

@Service
public class WrapperResourceManager {

    private Map<String, Wrapper> wrapperMap;

    @Value("${data}")
    private String dataPath;

    @Autowired
    private ResourceLoader resourceLoader;

    @PostConstruct
    private void init() {
	this.wrapperMap = readWrappers(dataPath);
    }

    @PreDestroy
    private void refreshData() {
	for (Wrapper wrapper : this.wrapperMap.values()) {
	    writeWrapper(wrapper, dataPath);
	}
    }

    public void add(Wrapper wrapper) {
	if (wrapper == null)
	    throw new SWException(ExceptionCode.InvalidInput);

	this.wrapperMap.put(wrapper.getName(), wrapper);

	refreshData();
    }

    public boolean exists(String id) {
	return wrapperMap.containsKey(id);
    }

    public Wrapper getWrapper(String id) {
	return wrapperMap.get(id);
    }

    public Collection<Wrapper> getAllWrapper() {
	return wrapperMap.values();
    }

    private void writeWrapper(Wrapper wrapper, String dataFolder) {
	if (wrapper != null) {
	    Gson gson = (new GsonBuilder()).setPrettyPrinting().create();
	    String wrapperJson = gson.toJson(wrapper, Wrapper.class);

	    try {
		FileUtils.writeStringToFile(new File(dataFolder + "\\" + wrapper.getName() + ".wrp"), wrapperJson);
	    }
	    catch (IOException e) {
		e.printStackTrace();
	    }

	}
    }

    private Map<String, Wrapper> readWrappers(String folderName) {
	if (StringUtils.isBlank(folderName)) {
	    throw new SWException(ExceptionCode.UnknownError);
	}

	Map<String, Wrapper> wrapperMap = new HashMap<String, Wrapper>();

	File dataFolder = new File(folderName);

	if (dataFolder == null || !dataFolder.exists() || !dataFolder.isDirectory()) {
	    throw new SWException(ExceptionCode.UnknownError);
	}

	File[] files = dataFolder.listFiles(new FilenameFilter() {

	    @Override
	    public boolean accept(File arg0, String arg1) {
		return arg1.endsWith(".wrp");
	    }
	});

	for (File file : files) {
	    Gson gson = new Gson();
	    try {
		Wrapper wrapper = gson.fromJson(new FileReader(file), Wrapper.class);
		System.out.println(wrapper.getName());
		wrapperMap.put(wrapper.getName(), wrapper);
	    }
	    catch (Exception e) {
		e.printStackTrace();
	    }
	}

	return wrapperMap;
    }

}
